package com.dong.common.lang.base64;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * Created by jianyang206881 on 2017/9/27.
 *
 * @since 1.8
 */
public class Base64Util {

    public static Base64.Encoder getEncoder() {
        return Base64
                .getEncoder();
    }

    public static Base64.Decoder getDecoder() {
        return Base64.getDecoder();
    }

    public static Base64.Encoder getUrlEncoder() {
        return Base64
                .getUrlEncoder();
    }

    public static Base64.Decoder getUrlDecoder() {
        return Base64.getUrlDecoder();
    }

    public static Base64.Encoder getMimeEncoder() {
        return Base64
                .getMimeEncoder();
    }

    public static Base64.Decoder getMimeDecoder() {
        return Base64.getMimeDecoder();
    }

    /*=======================often used=======================*/

    public static String encode(String src) {
        return Base64
                .getEncoder()
                .encodeToString(src.getBytes(StandardCharsets.UTF_8));
    }

    public static String decode(String encoded) {
        return new String(
                Base64.getDecoder().decode(encoded),
                StandardCharsets.UTF_8);
    }

    public static String urlEncode(String url) {
        return Base64
                .getUrlEncoder()
                .encodeToString(url.getBytes(StandardCharsets.UTF_8));
    }

    public static String urlDecode(String urlEncoded) {
        return new String(
                Base64.getUrlDecoder().decode(urlEncoded),
                StandardCharsets.UTF_8);
    }
}
